package m.jclecai.com.view.klpk;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.Checkable;
import android.widget.ImageView;

/**
 * Created by andy on 2017/6/14.
 * 这个是具有checked的状态的ImageView,具有这个状态，我们是可以使用Selector来实现；状态的改变；
 */

public class CheckedImageView extends ImageView implements Checkable {

    boolean isChecked=false;
    //定义状态集
    private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked};
    private Drawable mDrawableChecked;

    public CheckedImageView(Context context) {
        this(context,null);
    }

    public CheckedImageView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CheckedImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    @Override
    public void setChecked(boolean checked) {
        if (isChecked!=checked) {
            isChecked=checked;
            refreshDrawableState();
        }
    }


    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();
        //在这里我们将增加checked和unChecked的一个显示操作；
    }

    @Override
    public int[] onCreateDrawableState(int extraSpace) {
        int[] drawableState = super.onCreateDrawableState(extraSpace + 1);

        if(isChecked){
            mergeDrawableStates(drawableState,CHECKED_STATE_SET);
        }

        return drawableState;
    }

    @Override
    public boolean isChecked() {
        return isChecked;
    }

    @Override
    public void toggle() {
        setChecked(!isChecked);
    }
}
